package com.igeek.d0617;

import java.util.concurrent.Semaphore;

/**
 * @Author: zhiyu
 * @Date:18:48
 * @DESC:
 */
public class Test3 {
    private int count;
    Semaphore semaphoreA = new Semaphore(1);
    Semaphore semaphoreB = new Semaphore(0);
    Semaphore semaphoreC = new Semaphore(0);

    public Test3(int count) {
        this.count = count;
    }
    public void print(String string,Semaphore current,Semaphore next) throws InterruptedException {
        for (int i = 0; i < count; i++) {
            current.acquire();
            System.out.print(string);
            next.release();
        }
    }
    public void printA(){
        try {
            print("A",semaphoreA,semaphoreB);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public void printB(){
        try {
            print("B",semaphoreB,semaphoreC);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public void printC(){
        try {
            print("C",semaphoreC,semaphoreA);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        Test3 test3 = new Test3(10);
        new Thread(()->{
            test3.printA();
        },"线程1").start();
        new Thread(()->{
            test3.printB();
        },"线程2").start();
        new Thread(()->{
            test3.printC();
        },"线程3").start();
    }
}
